//
//  TouSuListViewController.m
//  阿帕
//
//  Created 阿帕 on 2019/12/21.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "TouSuListViewController.h"

#import "TouSuDetailViewController.h"

#import "TouSuListCell.h"

#import "SGPagingView.h"

#import "ZTXYiChangChuLiAlControllerViewController.h"

@interface TouSuListViewController ()<UITableViewDelegate,UITableViewDataSource,SGPageTitleViewDelegate>
{
    NSString *_abnormalFrom;
}
@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)UIImageView *foundImageView;

@property (nonatomic, assign) NSInteger pageNum;

@end

static NSString *const TouSuCellId = @"TousuListCellID";

static const CGFloat cellHeight = 120;

@implementation TouSuListViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _pageNum = 1;
        
    _isShowSegmentedControl = YES;
    
    self.view.backgroundColor = BackgroundColor;
    
    [self.view addSubview:self.tableView];
    
    [self setupBaseView];
}

- (void)setupBaseView {
    
    if (_isShowSegmentedControl) {
        [self createSegmentedControl];
    } else {
        self.navigationItem.title = @"投诉记录";
        _abnormalFrom = @"1";
        [self touSuListRequest];
    }
}

- (void)createSegmentedControl {
    NSArray *titleArr = @[@"我投诉的", @"投诉我的"];
    SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
    configure.indicatorStyle = SGIndicatorStyleCover;
    configure.indicatorColor = Theme_ColorValue;
    configure.titleColor = Theme_ColorValue;
    configure.indicatorHeight = SY(30);
    configure.indicatorAdditionalWidth = 120;
    configure.titleSelectedColor = [UIColor whiteColor];
    /// pageTitleView
    SGPageTitleView *pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, 0, 270, 30) delegate:self titleNames:titleArr configure:configure];
    pageTitleView.layer.borderWidth = 1;
    pageTitleView.layer.borderColor = Theme_ColorValue.CGColor;
    pageTitleView.layer.cornerRadius = 3;
    pageTitleView.layer.masksToBounds = YES;
    pageTitleView.backgroundColor = [UIColor whiteColor];
    self.navigationItem.titleView = pageTitleView;
}

- (void)touSuListRequest {
    WEAKSELF
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *params = nil;
    NSString *urlString = @"";
    
    if (_pagesFlag == 0) {
        params = @{
            @"pageNum": [NSNumber numberWithInteger:_pageNum],
            @"pageSize": DEFULT_LOAD_NUM,
            @"abnormalFrom": _abnormalFrom, //0->投诉司机 1->投诉货主
            @"orderDetailCode": _orderCode,
        };
        urlString = CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], ORDER_DETAIL_ABNORMAL);
    } else {
        params = @{
            @"pageNum": [NSNumber numberWithInteger:_pageNum],
            @"pageSize": DEFULT_LOAD_NUM,
            @"abnormalFrom": _abnormalFrom, //0->投诉司机 1->投诉货主
            @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
        };
        urlString = CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], ORDER_DETAIL_ABNORMAL_LIST);
    }
    
    [HTTPManager getNetworkRequest:urlString withParameter:params withVC:self netName:@"投诉列表" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {
            
            [self.dataArray addObjectsFromArray:[TouSuListModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            
            weakSelf.foundImageView.hidden = YES;
            weakSelf.tableView.hidden = NO;
            
        }else{
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
                weakSelf.tableView.hidden = YES;
            }
            
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

//按钮点击事件
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
    
    _pageNum = 1;
    
    switch (selectedIndex) {
        case 0:
//            投诉来源：0->投诉司机 1->投诉货主
            _abnormalFrom = @"1";
            [self touSuListRequest];
            break;
        case 1:
            _abnormalFrom = @"0";
            [self touSuListRequest];
            break;
            
        default:
            break;
    }
}


//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    [self touSuListRequest];
}
- (void)loadMoreData{
    _pageNum ++;
    [self touSuListRequest];
}

#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - NavHeight - 10);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TouSuListCell class]) bundle:nil] forCellReuseIdentifier:TouSuCellId];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    
    return _tableView;
}



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TouSuListCell *cell = [tableView dequeueReusableCellWithIdentifier:TouSuCellId];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TouSuListModel *model = self.dataArray[indexPath.row];
    
    return model.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    TouSuDetailViewController *tsdvc = [[TouSuDetailViewController alloc] init];
//    TouSuListModel *model = self.dataArray[indexPath.row];
//    tsdvc.touSuRecordsCode = model.code;
//    tsdvc.abnormalFrom = model.abnormalFrom;
//    [self.navigationController pushViewController:tsdvc animated:YES];
    ZTXYiChangChuLiAlControllerViewController *tsdvc = [[ZTXYiChangChuLiAlControllerViewController alloc] init];
    TouSuListModel *model = self.dataArray[indexPath.row];
    tsdvc.orderCode = model.code;
    tsdvc.abnormalFrom = model.abnormalFrom;
    [self.navigationController pushViewController:tsdvc animated:YES];
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.cy_centerX = self.view.cy_centerX;
        _foundImageView.cy_centerY = self.view.cy_centerY;
        _foundImageView.cy_centerY -= 60;
        [self.view addSubview:_foundImageView];
    }
    return _foundImageView;
}
@end
